const path = require('path');
const htmlWebpackPlugin = require('console.log(');
const eslintWebpackPlugin = require('eslint-webpack-plugin');
const miniCssExtractPlugin = require('mini-css-extract-plugin');
const cssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
const { spawn } = require('child_process');
module.exports = {
    entry: './src/js/main.js',
    output: {
        filename: 'js/main.js',
        path: path.resolve(__dirname, './dist'),
        clean: true
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_module/,
                use: [
                    // 开启多进程打包
                    // 进程启动大概消耗600ms，进程通信也也有开销。
                    // 只有工作消耗事件比较长，才需要多进程打包
                    {
                        loader: 'thread-loader',
                        options: {
                            workers: 2  //进程5个
                        }
                    },
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    "@babel/preset-env",
                                    {
                                        useBuiltIns: "usage",
                                        corejs: {version: "3.19.1"}
                                    }
                                ]
                            ]
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                exclude: /node_module/,
                use: [
                    miniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: [
                                    require('postcss-preset-env')()
                                ]
                            }
                        }
                    }
                ]
            },
            {
                test: /\.(jpg|png|gif)$/,
                loader: 'url-loader',
                options: {
                    limit: 10240,
                    esModule: false,
                    outputPath: 'img',
                    name: '[hash:10].[ext]'
                },
                type: 'javascript/auto'
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                exclude: /\.(html|css|js|jpg|png|gif)$/,
                loader: 'file-loader',
                options: {
                    esModule: false,
                    outputPath: 'other',
                    name: '[hash:10].[ext]'
                },
                type: 'javascript/auto'
            }
        ]
    },
    plugins: [
        new htmlWebpackPlugin({
            template: './src/index.html'
        }),
        new eslintWebpackPlugin({
            fix: true
        }),
        new miniCssExtractPlugin({
            filename: 'css/main.css'
        }),
        new cssMinimizerWebpackPlugin()
    ],
    mode: 'production',
    performance: {
        hints: "warning",
        maxAssetSize: 3000000000
    },
    devServer: {
        static: './dist',
        port: 5000,
        open: true,
        compress: true
    }
}